package com.controller;

import com.dao.TeamDao;
import com.domain.Team;
import com.server.TeamService;
import com.domain.Whour;
import com.dao.WhourDao;
import com.server.WhourService;
import com.domain.Employee;
import com.dao.EmployeeDao;
import com.server.EmployeeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.jvm.hotspot.memory.Space;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import org.springframework.ui.Model;
import java.util.List;
import java.util.ArrayList;

@Controller
@RequestMapping(value = "/team")
public class TeamController {

    @Resource(name = "employeeService")
    private EmployeeService employeeService;

    @Resource(name = "whourService")
    private WhourService whourService;

    @Resource(name = "teamService")
    private TeamService teamService;

    //通用页面跳转控制器
    @RequestMapping(value="/{formName}")
    public String f(@PathVariable String formName){
        return formName;
    }

    @RequestMapping(value="/04dengji")
    public String dengji04(){
        return "04在线登记";
    }

    //根据Id查询
    @RequestMapping(value="/10Team")
    public String findNameByTeamId(Integer team_id,Model model){
        Team team= teamService.findNameByTeamId(team_id);
        System.out.println("----------------------");
        model.addAttribute("team",team);
        return  "redirect:10team";
    }

    @RequestMapping(value="/select",method = RequestMethod.POST)
    public String select(@RequestParam("name") String name,@RequestParam("leader")String leader,
                         Model model,HttpSession session){
        //根据负责人，名称进行模糊查询
        System.out.println("==============="+name+leader);
        List<Team> selectlist=teamService.teamSelect(name,leader);
        System.out.println("=============="+selectlist);
        model.addAttribute("selectlist",selectlist);
        return "10team";
    }


    //显示所有团队信息
    @RequestMapping(value="/showTeam")
    public String showTeam(Model model){
        List<Team> teamList= teamService.showTeam();
        System.out.println("----------------------");
        model.addAttribute("teamList",teamList);

     //   int teamTotal=teamService.findTeamTotal();


//        //计算总页数
//        if(teamTotal==0){
//            model.addAttribute("all",0);
//        }else{
//            model.addAttribute("all",teamTotal);
//        }
//        //计算总页数
//        if((teamTotal/pageSize)<1){
//            model.addAttribute("all",1);
//        }else {
//            if (teamTotal % pageSize!=0) {
//                int pageTotal = new Integer(teamTotal / pageSize + 1);
//                model.addAttribute("all", pageTotal);
//            } else {
//                int pageTotal = new Integer(teamTotal / pageSize);
//                model.addAttribute("all", pageTotal);
//            }
//        }


        return  "10team";
    }

    @RequestMapping(value="/deleteByTeam")
    public String deleteByTeam(@Param("team_id") int team_id)throws Exception{
        System.out.println("id======"+team_id);
        teamService.deleteByTeamId(team_id);
        return "redirect:showTeam?id="+team_id;
    }

    //退出团队操作
    @RequestMapping(value="/outTeam")
    public String outTeam(@RequestParam("number")String number,@RequestParam("leader")String leader,
                          @RequestParam("teamId") Integer team_id,@RequestParam("father")int father,
                          Model model,HttpSession session)throws Exception{
        Employee ee=employeeService.findEmployeeByNumber(number);
        String name=ee.getName();
        if (team_id != father && name.equals(leader)) {
            //如果子团队和父团队不一致且队长为该员工时
            teamService.deleteTeamByLeader(leader);//根据队长删除团队信息
            whourService.deleteWhourByTeamId (team_id);//根据团队id删除该团队所有员工工时表
        } else if (team_id != father && name != leader) {
            //如果子团队和父团队不一致且该员工不为队长时
            whourService.deleteWhourByEmpNumber(team_id, number );
        } else if (team_id != father && name.equals(leader)) {
            //子节点和父节点一致，该员工为队长时
            teamService.deleteTeamByLeader (leader);//根据队长删除团队信息
            whourService.deleteWhourByTeamId (team_id);//根据team_id删除所有员工工时表
        } else if (team_id == father && !(name.equals(leader))) {
            //子节点和父节点一致，该员工不为队长时
            whourService.deleteWhourByTeamId (team_id );//根据team_id删除该员工工时表
        }
        Employee employee = employeeService.findEmployeeByNumber ( number );
       // session.setAttribute("teamList",teamList);
        //model.addAttribute("teamList",teamList);
        session.setAttribute ( "employee", employee );
        model.addAttribute ( "employee", employee );
        model.addAttribute ( "employee", employee );
        return "redirect:08Employee";
    }

}
